package com.jzr.open.db1.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzr.open.common.SecurityHelper;
import com.jzr.open.db1.mapper.TDoctorMapper;
import com.jzr.open.db1.mapper.TNurseMapper;
import com.jzr.open.db1.mapper.TShiftPlanMapper;
import com.jzr.open.db1.model.TDoctor;
import com.jzr.open.db1.model.TNurse;
import com.jzr.open.db1.model.TShiftPlan;
import com.jzr.open.db1.service.ShiftPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShiftPlanServiceImpl extends ServiceImpl<TShiftPlanMapper,TShiftPlan> implements ShiftPlanService {
    @Autowired
    private TDoctorMapper doctorMapper;

    @Autowired
    private TNurseMapper nurseMapper;

    public List<Integer> getIdList(Wrapper wrapper) {
        return baseMapper.getIdList(wrapper);
    }

    public Integer getId(Wrapper wrapper) {
        List<Integer> list = getIdList(wrapper);
        return list == null || list.size() <= 0 ? null : list.get(0);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(List<TShiftPlan> list) {
        if(list == null || list.isEmpty()){
            return 0;
        }
        Integer userId = SecurityHelper.getUserId();
        int count = 0;
        for(TShiftPlan record : list){
            QueryWrapper<TShiftPlan> qw = new QueryWrapper<>();
            qw.eq(TShiftPlan.StaffType_,record.getStaffType())
                    .eq(TShiftPlan.StaffNo_,record.getStaffNo())
                    .eq(TShiftPlan.Y_,record.getY())
                    .eq(TShiftPlan.M_,record.getM());

            setStaffId(record);
            record.setUpdateBy(userId);
            record.setUpdateTime(LocalDateTime.now());
            Integer id = getId(qw);
            if(id == null){
                count += baseMapper.insert(record);
            }else{
                record.setId(id);
                count += baseMapper.updateById(record);
            }
        }
        return count;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(List<TShiftPlan> list) {
        if(list == null || list.isEmpty()){
            return 0;
        }
        int count = 0;
        for(TShiftPlan record : list){
            QueryWrapper<TShiftPlan> qw = new QueryWrapper<>();
            qw.eq(TShiftPlan.StaffType_,record.getStaffType())
                    .eq(TShiftPlan.StaffNo_,record.getStaffNo())
                    .eq(TShiftPlan.Y_,record.getY())
                    .eq(TShiftPlan.M_,record.getM());
            count += baseMapper.delete(qw);
        }
        return count;
    }

    private void setStaffId(TShiftPlan record){
        List<Integer> list = null;

        if(record.getStaffType().intValue() == 0){
            QueryWrapper<TNurse> qw = new QueryWrapper<>();
            qw.eq(TNurse.DeptCode_,record.getDeptCode())
                    .eq(TNurse.Code_,record.getStaffNo());
            list = nurseMapper.getIdList(qw);

            record.setStaffId(list == null || list.size()<=0 ? null :list.get(0));

        }else if(record.getStaffType().intValue() == 1){
            QueryWrapper<TDoctor> qw = new QueryWrapper<>();
            qw.eq(TDoctor.DeptCode_,record.getDeptCode())
                    .eq(TDoctor.Code_,record.getStaffNo());

            list = doctorMapper.getIdList(qw);
            record.setStaffId(list == null || list.size()<=0 ? null :list.get(0));
        }

    }
}
